/*
 * This file defines the functions and data structures 
 * relating to calculating player stats and parsing the batting.txt file
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NUMBER_OF_PLAYERS 29
#define NAME_SIZE 30
#define COUNTRY_SIZE 5
/* 
 * Represents a cricket player
 */
struct cricket_player{
	char  name[NAME_SIZE];					// Cricket players name
	char  country[COUNTRY_SIZE];
	int innings;
	int runs;
	int times_not_out;
	int highest_score;
	int highest_score_is_not_out; 	// Boolean
	float batting_average; 			// The total number of runs divided by the total number of innings in which the batsman was out. Ave = Runs/[I - NO] 
	int access_count; 				// How many times the player has been accesed 
} players[NUMBER_OF_PLAYERS];

int get_player(char * player_name, struct cricket_player * Player);
void init();
/*
 * Call this function to load the cricket players into memory
 */

int count;
void init(){
	FILE *file;
	char *mode = "rt";
	char outputFilename[] = "battings.txt";
	count = 0;
	// Open the file
	file = fopen(outputFilename, mode);

	if (file == NULL) {
		fprintf(stderr, "Can't open output file %s!\n", outputFilename);
		exit(1);
	}

	// parse the file
  	int buffer_size = 200;
	
  	char line_buffer[buffer_size];  
  	while( fgets(line_buffer, buffer_size, file) != NULL ){      
		struct cricket_player player;		
	
		if(line_buffer[0] != '#'){ // skip comments
			char * token;
			token = strtok(line_buffer," ;\t\n");
			
			int coloumn = 1;
			while (token != NULL){ 				
				switch(coloumn){
					case 1 :memset(player.name,'\0',NAME_SIZE); 
							memcpy(player.name,token,strlen(token));					
																	break;
					case 2 :memset(player.country,'\0',COUNTRY_SIZE); 
							memcpy(player.country,token,strlen(token));				
																	break;
					case 3 : player.innings = atoi(token); 			break;
					case 4 : player.runs = atoi(token); 			break;
					case 5 : player.times_not_out = atoi(token); 	break;
					case 6 : player.highest_score = atoi(token); 	break; //TODO refactor
					case 7 : if(strcmp("TRUE\n", token) == 0) player.highest_score_is_not_out = 1; else player.highest_score_is_not_out = 0; break; 
				}
				
				//printf("Token: %s\n", token);

				token = strtok(NULL, " ;\t\n");
				coloumn++;
			}

			player.access_count = 0;			

			//Calculate player stats	
			float factor = (player.innings - player.times_not_out);
			if(factor > 0)			
				player.batting_average = player.runs/factor;
			else
				player.batting_average = 0;
				
			//Add player to the list of players
			
			players[count] = player;
			count++;
      	}
	}
    
  	//Close the file
  	fclose(file);

} //init()

/*
 * Function to get a cricket player by name
 * 
 * Returns:   
 * 		If the player exists: the player with the given name.
 * 		If no player was found an empty player is returned
 */


int get_player(char * player_name, struct cricket_player * Player){
	int i;
	struct cricket_player * p;
	for(i = 0; i < count; i++){
		p = &players[i];
		if(strcmp(p->name, player_name) == 0)
		{ //If player is found
			p->access_count++;
			*Player = *p;
			return 1;
			
		}
	} 
	return 0;
}

/* Only for testing */
/*int main(void){
	init();

	char * name = "Black";
	struct cricket_player player;
	get_player(name);
	get_player(name);
	get_player(name);
	player = get_player(name);

	printf("Players name: (should be: Black) is:%s\n", 					player.name);
	printf("Players country: (should be: WI) is:%s\n", 					player.country);
	printf("Players innings: (should be: 6) is:%d\n", 					player.innings);
	printf("Players runs: (should be: 6)  is:%d\n",	 					player.runs);
	printf("Players times_not_out: (should be: 2) is:%d\n",				player.times_not_out);
	printf("Players highest_score: (should be: 3) id:%d\n", 			player.highest_score);
	printf("Players highest_score_is_not_out: (should be: 1) is:%d\n", 	player.highest_score_is_not_out);
	printf("Players batting average: (should be: 18.25) is:%f\n", 		player.batting_average);
	printf("Players access count: (should be: 4) is:%d\n", 				player.access_count);
	
	exit(EXIT_SUCCESS);
}*/

